package com.hzt.agprosaleplatform.controller;

import com.hzt.agprosaleplatform.common.CustomResponse;
import com.hzt.agprosaleplatform.dto.MemorySendMessageDTO;
import com.hzt.agprosaleplatform.service.MemoryChatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 内存存储的聊天控制器
 */
@RestController
@CrossOrigin(origins = "http://localhost:5173")
@RequestMapping("/memory-chat")
public class MemoryChatController {

    @Autowired
    private MemoryChatService memoryChatService;

    /**
     * 买家联系客服（创建会话）
     */
    @PostMapping("/contact")
    public CustomResponse contactCustomerService(@RequestParam String buyerId, 
                                                 @RequestParam String shopId) {
        return memoryChatService.contactCustomerService(buyerId, shopId);
    }

    /**
     * 获取买家的会话列表
     */
    @GetMapping("/buyer/sessions/{buyerId}")
    public CustomResponse getBuyerSessions(@PathVariable String buyerId) {
        return memoryChatService.getBuyerSessions(buyerId);
    }

    /**
     * 获取店铺的会话列表
     */
    @GetMapping("/shop/sessions/{shopId}")
    public CustomResponse getShopSessions(@PathVariable String shopId) {
        return memoryChatService.getShopSessions(shopId);
    }

    /**
     * 发送消息
     */
    @PostMapping("/message")
    public CustomResponse sendMessage(@RequestBody MemorySendMessageDTO sendMessageDTO) {
        return memoryChatService.sendMessage(sendMessageDTO);
    }

    /**
     * 获取会话消息列表
     */
    @GetMapping("/messages/{sessionId}")
    public CustomResponse getSessionMessages(@PathVariable String sessionId) {
        return memoryChatService.getSessionMessages(sessionId);
    }

    /**
     * 标记消息为已读
     */
    @PutMapping("/message/{messageId}/read")
    public CustomResponse markMessageAsRead(@PathVariable String messageId, 
                                           @RequestParam String readerId) {
        return memoryChatService.markMessageAsRead(messageId, readerId);
    }

    /**
     * 获取买家未读消息数量
     */
    @GetMapping("/buyer/unread/{buyerId}")
    public CustomResponse getBuyerUnreadCount(@PathVariable String buyerId) {
        return memoryChatService.getBuyerUnreadCount(buyerId);
    }

    /**
     * 获取店铺未读消息数量
     */
    @GetMapping("/shop/unread/{shopId}")
    public CustomResponse getShopUnreadCount(@PathVariable String shopId) {
        return memoryChatService.getShopUnreadCount(shopId);
    }

    /**
     * 关闭会话（清理内存数据）
     */
    @DeleteMapping("/session/{sessionId}")
    public CustomResponse closeSession(@PathVariable String sessionId) {
        return memoryChatService.closeSession(sessionId);
    }
}